<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="searchForm.orderNo" placeholder="请输入订单编号" clearable />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="searchForm.productName" placeholder="请输入产品名称" clearable />
      </el-form-item>
      <el-form-item label="排产状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="待排产" value="PENDING" />
          <el-option label="已排产" value="SCHEDULED" />
          <el-option label="生产中" value="PRODUCING" />
          <el-option label="已完成" value="COMPLETED" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleSchedule">订单排产</el-button>
      <el-button type="success" :icon="Refresh" @click="handleAutoSchedule">自动排产</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="orderNo" label="订单编号" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="quantity" label="订单数量" />
      <el-table-column prop="plannedStartTime" label="计划开始时间" />
      <el-table-column prop="plannedEndTime" label="计划完成时间" />
      <el-table-column prop="productionLine" label="分配产线" />
      <el-table-column prop="priority" label="优先级">
        <template #default="scope">
          <el-tag :type="getPriorityType(scope.row.priority)">{{ getPriorityText(scope.row.priority) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="排产状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="scope">
          <el-button type="primary" link :icon="EditPen" @click="handleEdit(scope.row)">调整排产</el-button>
          <el-button type="warning" link :icon="Clock" @click="handleReschedule(scope.row)">重新排产</el-button>
          <el-button type="danger" link @click="handleCancel(scope.row)">取消排产</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="orderScheduling">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { CirclePlus, EditPen, Clock, Refresh } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  orderNo: "",
  productName: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取优先级类型
const getPriorityType = (priority: string) => {
  const priorityMap: Record<string, string> = {
    HIGH: "danger",
    MEDIUM: "warning",
    LOW: "info"
  };
  return priorityMap[priority] || "info";
};

// 获取优先级文本
const getPriorityText = (priority: string) => {
  const priorityMap: Record<string, string> = {
    HIGH: "高",
    MEDIUM: "中",
    LOW: "低"
  };
  return priorityMap[priority] || priority;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    PENDING: "warning",
    SCHEDULED: "primary",
    PRODUCING: "success",
    COMPLETED: "info"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    PENDING: "待排产",
    SCHEDULED: "已排产",
    PRODUCING: "生产中",
    COMPLETED: "已完成"
  };
  return statusMap[status] || status;
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // TODO: 调用API获取订单排产数据
    console.log("获取订单排产数据");
  } catch (error) {
    console.error("获取订单排产列表失败:", error);
    ElMessage.error("获取订单排产列表失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 订单排产
const handleSchedule = () => {
  ElMessage.info("订单排产功能待实现");
};

// 自动排产
const handleAutoSchedule = () => {
  ElMessage.info("自动排产功能待实现");
};

// 调整排产
const handleEdit = (row: any) => {
  ElMessage.info("调整排产功能待实现");
};

// 重新排产
const handleReschedule = (row: any) => {
  ElMessage.info("重新排产功能待实现");
};

// 取消排产
const handleCancel = (row: any) => {
  ElMessageBox.confirm(`确认取消订单 "${row.orderNo}" 的排产吗？`, "取消确认", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage.success("取消排产成功");
      getTableData();
    })
    .catch(() => {});
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}
</style>